package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.ETakeDrugs;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.User;
import com.api.shopapi.service.ETakeDrugsService;
import com.api.shopapi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("ETakeDrugs")
public class ETakeDrugsController extends BaseController{

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("ETakeDrugsServiceImplOne")
    private ETakeDrugsService takeDrugsService;



    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String userCode = jsonObject.getString("userCode");
            String elderlyCode = jsonObject.getString("elderlyCode");



            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/ETakeDrugs/Add");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);
            String drugName = jsonObject.getString("drugName");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String beginTimeStr = jsonObject.getString("beginTime");
            Date beginTime = sdf.parse(beginTimeStr);
            String endTimeStr = jsonObject.getString("endTime");
            Date endTime = sdf.parse(endTimeStr);
            String dosage = jsonObject.getString("dosage");
            String treatment = jsonObject.getString("treatment");
            String takeMethod = jsonObject.getString("takeMethod");
            String remarks = jsonObject.getString("remarks");

            ETakeDrugs takeDrugs=new ETakeDrugs();
            takeDrugs.setETakeDrugsCode(UUID.randomUUID().toString());
            takeDrugs.setUserCode(userCode);
            takeDrugs.setElderlyCode(elderlyCode);
            takeDrugs.setElderlyName(user.getName());
            takeDrugs.setDrugName(drugName);
            takeDrugs.setBeginTime(beginTime);
            takeDrugs.setEndTime(endTime);
            takeDrugs.setDosage(dosage);
            takeDrugs.setTakeMethod(takeMethod);
            takeDrugs.setTreatment(treatment);
            takeDrugs.setRemarks(remarks);
            takeDrugs.setCreateTime(new Date());
            takeDrugs.setStaffCode(staffCode);
            takeDrugs.setStatus(1);
            takeDrugs.setGovernmentCode(user.getGovernmentCode());

            takeDrugsService.insert(takeDrugs);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ETakeDrugs/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String elderlyCode = jsonObject.getString("elderlyCode");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");



            PreCheck preCheck=checkSignAdmInterface(timeStr,sign,staffCode,"/ETakeDrugs/Page");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            PageInfo<ETakeDrugs> takeDrugsPageInfo = takeDrugsService.getPageListByElderlyCode(pageIndex, pageSize, elderlyCode);

            return success(takeDrugsPageInfo);
        } catch (Exception e) {
            insertError("/ETakeDrugs/Page",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }




}
